const app = getApp();
Page({
    data: {
        wxOpenId: '',
        selectAreaType: '',
        choiceAreaListIndex: 0, // 选择多列时的index
        sexArray: [
            {name: '男',value: 0},
            {name: '女',value: 1}
        ],
        jobList: [
            {name: '省区经理',value: 1},
            {name: '地区经理',value: 2},
            {name: '业务员',value: 3}
        ],
        productList: [
            {name: '黄金单品',value: 1,checked: false},
            {name: '普药',value: 2,checked: false},
        ],
        productLists: [
            {name: '黄金单品', value: 1, checked: false},
            {name: '普药', value: 2, checked: false},
        ],
        areaList: [],//省市区 总数据
        columns: [],//多级联动省市区数据
        info: {
            gender: 0,
            realName: '',
            phone: '',
            investmentCapacity: '', // 出资能力
            formerCompanyHonours: [], // 原就职业and 辉煌
            formerPosition: '', // 原职位
            expectPosition: '', // 期望职位
            expectOperateAddress: [], // 地址组合
            formerOperateType: '', //原作品类型
            expectOperateType: '',  // 期望作品类型
        },
        areaShow: false
    },
    onLoad () {
        this.setData({wxOpenId: wx.getStorageSync('wxOpenId')})
        this.getAreaTree();
        this.getUserInfo();
    },
    submit() {
        let {info} = this.data
        const regPhone = /^1(3|4|5|6|7|8|9)\d{9}$/
        info.expectOperateType = String(info.expectOperateType)
        info.formerOperateType = String(info.formerOperateType)
        info.wxOpenId = this.data.wxOpenId
        info.investmentCapacity = Number(info.investmentCapacity || false)
        if (!info.realName) {
            app.dialogJs.tip('请输入姓名')
            return
        }
        if (!info.phone) {
            app.dialogJs.tip('请输入手机号码')
            return
        }
        if (!regPhone.test(info.phone)) {
            app.dialogJs.tip('请输入正确的手机号')
            return
        }
        if (!info.formerCompanyHonours || (info.formerCompanyHonours && !info.formerCompanyHonours.length)) {
            app.dialogJs.tip('请添加填写曾就职企业和辉煌战绩')
            return
        }
        if (info.formerCompanyHonours) {
            let status = true
            for (let k = 0; k < info.formerCompanyHonours.length; k++) {
                if (!info.formerCompanyHonours[k].formerComany || !info.formerCompanyHonours[k].formerHonour) {
                    status = false
                    break;
                }
            }
            if (!status) {
                app.dialogJs.tip('填写的曾就职企业或辉煌战绩不能为空')
                return
            }
        }
        if (!info.formerPosition) {
            app.dialogJs.tip('请选择原职位')
            return
        }
        if (!info.expectPosition) {
            app.dialogJs.tip('请选择期望职位')
            return
        }
        if (!info.expectOperateAddress || (info.expectOperateAddress && !info.expectOperateAddress.length)) {
            app.dialogJs.tip('请添加期望地区')
            return
        }
        if(!info.formerOperateType){
            app.dialogJs.tip('请选择曾操作品类')
            return
        }
        if(!info.expectOperateType){
            app.dialogJs.tip('请选择期望操作品类')
            return
        }
        let t = true
        for (let k = 0; k < info.expectOperateAddress.length; k++) {
            if (!info.expectOperateAddress[k].expectProvinceName) {
                t = false
                break
            }
        }
        if (!t) {
            app.dialogJs.tip('请选择地址')
            return
        }

        wx.showLoading()
        app.postRequest('/user/updateUserInfo', info).then(r => {
            wx.hideLoading()
            if (r.code == 0) {
                app.dialogJs.tip(r.message)
                wx.redirectTo({
                    url:"/pages/userDetail/userDetail"
                })
            } else {
                app.dialogJs.tip(r.message)
            }
        })
    },
    showChoiceA: function () {
        this.setData({
            areaShow: true
        })
    },
    bindInput: function (e) {
        let n = e.currentTarget.dataset.name || '', index = e.currentTarget.dataset.index || 0,
            iName = e.currentTarget.dataset.itemname || ''
        let {info} = this.data
        if (iName) {
            info[n][index][iName] = e.detail.value
        } else {
            info[n] = e.detail.value
        }
        this.setData({
            info
        })
    },
    // 添加区域
    addChoiceArea() {
        let {info: {expectOperateAddress}} = this.data
        expectOperateAddress.push({
            expectProvince: '',
            expectProvinceName: '',
            expectCity: '',
            expectCityName: '',
            expectCountry: '',
            expectCountryName: ''
        })
        this.setData({
            'info.expectOperateAddress': expectOperateAddress
        })
    },
    onChange({detail}) {
        let {info, info: {investmentCapacity}} = this.data
        info.investmentCapacity = !!detail
        this.setData({
            info
        })
    },
    // 选择作品类
    choiceClass(e) {
        let type = e.currentTarget.dataset.type || ''
        let m = this.data.info[type] || []
        let val = e.currentTarget.dataset.value
        let index = e.currentTarget.dataset.index
        let l = type == 'expectOperateType' ? 'productLists' : 'productList'
        const {info} = this.data
        let arr = this.data[l]
        if (arr[index].checked) {
            arr[index].checked = false
        } else {
            arr[index].checked = true
        }
        if (m && m.length) {
            if (!m.includes(val + '')) {
                m.push(val + '')
            } else {
                m = m.filter(r => r != val)
            }
        } else {
            m.push(val)
        }
        info[type] = m
        let obj = {}
        obj[l] = arr
        obj['info'] = info
        this.setData(obj)
    },
    // 选择 性别
    choice: function (e) {
        let info = this.data.info
        info.gender = e.currentTarget.dataset.value
        this.setData({
            info
        })
    },
    // 添加 曾就职企业
    addHistory: function () {
        let {data: {info, info: {formerCompanyHonours}}} = this
        formerCompanyHonours.push({
            formerComany: '',
            formerHonour: ''
        })
        info.formerCompanyHonours = formerCompanyHonours
        this.setData({
            info
        })
    },
    getJob(e) {
        let value = e.currentTarget.dataset.value || '',
            names = e.currentTarget.dataset.name || '';
        let k = this.data.info
        k[names] = value
        if(names == 'expectPosition'){
            if (value == 1) {
                k['expectOperateAddress'] = [{
                    expectProvince: '',
                    expectCity: '',
                    expectCountry: '',
                    expectProvinceName: '',
                    expectCityName: '',
                    expectCountryName: ''
                }]
            } else {
                k['expectOperateAddress'] = []
            }
        }
        this.setData({
            info: k
        })
    },
    onAreaChange(event) {
        const {selectAreaType} = this.data
        const {picker, value, index} = event.detail;
        if (selectAreaType == 'province') {
            return
        }
        if (selectAreaType == 'city') {
            if (index === 0) {//改变的是省那列
                picker.setColumnValues(1, value[0].citySet);
            }
        }else if (selectAreaType == 'area'){
            if (index === 0) {//改变的是省那列
                picker.setColumnValues(1, value[0].citySet);
                picker.setColumnValues(2, value[0].citySet[0].distirctSet);
            } else if (index === 1) {//改变的是市那列
                picker.setColumnValues(2, value[1].distirctSet);
            }
        }
    },
    /*省市区 取消*/
    onAreaCancel(event) {
        this.setData({areaShow: false})
    },
    onAreaConfirm(event) {
        let {selectAreaType, choiceAreaListIndex, info, info: {expectOperateAddress}} = this.data
        this.onAreaCancel();
        let res = event.detail.value;

        if (selectAreaType == 'province') {
            expectOperateAddress = []
            let infoss = {}
            infoss['expectProvince'] = res[0].id
            infoss['expectCity'] = ''
            infoss['expectCountry'] = ''
            infoss['expectProvinceName'] = res[0].name
            infoss['expectCityName'] = ''
            infoss['expectCountryName'] = ''
            expectOperateAddress = [infoss]
        }else if(selectAreaType == 'city'){
            expectOperateAddress[choiceAreaListIndex] = {
                expectProvince: res[0].id,
                expectCity: res[1].id,
                expectProvinceName: res[0].name,
                expectCityName: res[1].name,
                expectCountryName: '',
                expectCountry: ''
            }
        }else if(selectAreaType == 'area'){
            expectOperateAddress[choiceAreaListIndex] = {
                expectProvince: res[0].id,
                expectCity: res[1].id,
                expectProvinceName: res[0].name,
                expectCityName: res[1].name,
                expectCountryName: res[2].name,
                expectCountry: res[2].id
            }
        }
        console.log(info)
        info['expectOperateAddress'] = expectOperateAddress
        this.setData({
            info: info
        })
    },
    onCloseDialog() {
        this.setData({dialogflagShow: false})
    },
    /*获取省市区*/
    getAreaTree() {
        app.getRequest('/user/getAddressTree').then((res) => {
            this.setData({areaList: res.data.area});
        })
    },
    selectArea(event) {
        this.setData({ columns:[] })
        let defaultIndex = [];
        let types = event.currentTarget.dataset.type || '' // province   city   area
        let indexs = event.currentTarget.dataset.index || 0;
        let item = event.currentTarget.dataset.item;//当前行选中的地区
        this.setData({
            selectAreaType: types,
            choiceAreaListIndex: indexs
        })
        this.data.areaList.forEach((valP, indexP) => {
            if (valP.id == item.expectProvince) {
                defaultIndex.push(indexP);
                valP.citySet.forEach((valC, indexC) => {
                    if (valC.id == item.expectCity) {
                        defaultIndex.push(indexC);
                        valC.distirctSet.forEach((valA, indexA) => {
                            if (valA.id == item.expectCountry) {
                                defaultIndex.push(indexA);
                            }
                        })
                    }
                })
            }
        })
        if (defaultIndex.length === 0) {
            defaultIndex = [0, 0, 0];
        }
        let columns = []
        if (types === 'province') {
            columns = [
                {
                    values: this.data.areaList,
                    defaultIndex: defaultIndex[0]
                }
            ]
        }else if (types === 'city') {
            columns = [
                {
                    values: this.data.areaList,
                    defaultIndex: defaultIndex[0]
                },
                {
                    values: this.data.areaList[defaultIndex[0]].citySet,
                    defaultIndex: defaultIndex[1]
                }]
        }else if (types === 'area'){
            columns = [
                {
                    values: this.data.areaList,
                    defaultIndex: defaultIndex[0]
                },
                {
                    values: this.data.areaList[defaultIndex[0]].citySet,
                    defaultIndex: defaultIndex[1]
                },
                {
                    values: this.data.areaList[defaultIndex[0]].citySet[defaultIndex[1]].distirctSet,
                    defaultIndex: defaultIndex[2]
                }]
        }
        this.setData({columns, areaShow: true})
    },
    showDelete(e) {
        let _this = this
        app.dialogJs.modalConfirm('确定要删除么？').then(r => {
            let index = e.currentTarget.dataset.index || 0
            let {info, info: {expectOperateAddress}} = _this.data
            expectOperateAddress.splice(index, 1)
            info['expectOperateAddress'] = expectOperateAddress
            _this.setData({info})
        })
        this.setData({
            areaShow: false
        })
    },
    // 删除曾就职企业
    delFormerCompany(e){
        let _this = this
        app.dialogJs.modalConfirm('确定要删除这条数据么？').then(r => {
            let index = e.currentTarget.dataset.index || 0
            let {info, info: {formerCompanyHonours}} = _this.data
            formerCompanyHonours.splice(index, 1)
            info['formerCompanyHonours'] = formerCompanyHonours
            _this.setData({info})
        })
    },
    getUserInfo() {
        wx.showLoading()
        app.getRequest('/user/getUserInfo', {wxOpenId: this.data.wxOpenId}).then(res => {
            wx.hideLoading()
            if (res.code == 0) {
                let info = this.data.info;
                for (let key in info) {
                    info[key] = res.data[key];
                }
                // 辉煌战绩
                if(info['formerCompanyHonours'] == null || info['formerCompanyHonours'].length == 0){
                    info['formerCompanyHonours'] = [{formerComany: '',formerHonour: ''}]
                }
                info['formerOperateType'] = info['formerOperateType'] && info['formerOperateType'].split(',') || []
                info['expectOperateType'] = info['expectOperateType'] && info['expectOperateType'].split(',') || []
                let {productList, productLists} = this.data
                if (info['formerOperateType'] && info['formerOperateType'].length) {
                    productList && productList.length && productList.forEach(rr => {
                        if (info['formerOperateType'].includes(rr.value + '')) {
                            rr.checked = true
                        }
                    })
                }
                if (info['expectOperateType'] && info['expectOperateType'].length) {
                    productLists.forEach(rr => {
                        if (info['expectOperateType'].includes(rr.value + '')) {
                            rr.checked = true
                        }
                    })
                }
                info.investmentCapacity = !!info.investmentCapacity
                console.log(info)
                this.setData({
                    info,
                    productList,
                    productLists
                })
            }
        })
    }
})
